package com.example.coolweather.util;

import android.content.Context;
import android.text.TextUtils;

import com.example.coolweather.db.CoolWeatherDB;
import com.example.coolweather.gson.Weather;
import com.example.coolweather.model.City;
import com.example.coolweather.model.County;
import com.example.coolweather.model.Province;
import com.google.gson.Gson;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class Utility {
    public static boolean handleProvinceResponse(String response, Context context) {
        if (!TextUtils.isEmpty(response)) {
            JSONArray allProvinces = null;
            try {
                allProvinces = new JSONArray(response);
                for (int i = 0; i < allProvinces.length(); i++) {
                    JSONObject provinceObject = allProvinces.getJSONObject(i);
                    Province province = new Province();
                    province.setProvinceName(provinceObject.getString("name"));
                    province.setProvinceCode(provinceObject.getString("id"));
                    CoolWeatherDB db = CoolWeatherDB.getInstance(context);
                    db.saveProvince(province);
                }
                return true;
            } catch (JSONException e) {
                e.printStackTrace();
            }


        }
        return false;
    }


    public static boolean handleCityResponse(String response,int provinceId,Context context){
        if(!TextUtils.isEmpty(response)){
            JSONArray allCities= null;
            try {
                allCities = new JSONArray(response);
                for(int i=0;i<allCities.length();i++){
                    JSONObject cityObject=allCities.getJSONObject(i);
                    City city=new City();
                    city.setCityName(cityObject.getString("name"));
                    city.setCityCode(cityObject.getString("id"));
                    city.setProvinceId(provinceId);
                    CoolWeatherDB db=CoolWeatherDB.getInstance(context);
                    db.saveCity(city);
                } return true;
            } catch (JSONException e) {
                e.printStackTrace();
            }

        }
        return false;
    }



    public static boolean handleCountyResponse(String response,int cityId,Context context){
        if (!TextUtils.isEmpty(response)){
            JSONArray allCounties= null;
            try {
                allCounties = new JSONArray(response);
                for (int i=0;i<allCounties.length();i++){
                    JSONObject countyObject=allCounties.getJSONObject(i);
                    County county=new County();
                    county.setCountyName(countyObject.getString("name"));
                    county.setCountycode(countyObject.getString("weather_id"));
                    county.setCityId(cityId);
                    CoolWeatherDB db=CoolWeatherDB.getInstance(context);
                    db.saveCounty(county);
                }
                return true;
            } catch (JSONException e) {
                e.printStackTrace();
            }

        }
        return false;
    }


    public static Weather handleWeatherResponse (String response){
        JSONObject jsonObject = null;
        try {
            jsonObject = new JSONObject(response);
            JSONArray jsonArray=jsonObject.getJSONArray("HeWeather");
            String weatherContent =jsonArray.getJSONObject(0).toString();
            return new Gson().fromJson(weatherContent,Weather.class);
        } catch (JSONException e) {
            e.printStackTrace();

        }
       return null;
    }

}
